<?php

namespace App\Http\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;

class MemberDoctor extends BaseModel
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    protected $table = 'member_doctor';
    public $timestamps = true;


    protected $fillable = [
        'id',
        'user_id',
        'real_name',
        'unit',
        'department',
        'title',
        'phone',
        'id_card_number',
        'certificate',
        'ip',
        'bank_number',
        'opening_bank',
        'status',
        'reason',
        'examine_time',
        'sort'
    ];

    protected $hidden = [
        'created_at',
        'updated_at',
        'deleted_at',
        'sort',
        'user_id'
    ];

    /**
     * 类型转换
     *
     * @var array
     */
    protected $casts = [
        'address' => 'array',
    ];

    public function getStatusTextAttribute($value)
    {
        // $state = ['未认证', '已认证', '认证通过', '驳回'];
        $state = ['未认证', '已申请', '已认证', '驳回'];
        return $state[$value];
    }

    /**
     * 医师资格认证
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-03-18
     * @param array $payload
     * @param obj $user
     * @return mixed
     */
    public static function doctorCert($payload, $user)
    {
        if ($user['doctor_status'] == 2) {
            return fail_message('您已经认证成功，无需重复认证');
        }
        $payload['ip'] = real_ip();
        $payload['status'] = 1;
        if (MemberDoctor::updateOrCreate([
            'user_id' => $user['userid'],
        ], $payload)) {
            $user->doctor_status = 1;
            $user->save();
            return true;
        } else {
            return false;
        }
    }
}
